<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';

//Người tạo : Hoàng Hải Luân - Team 0
//Module 	: MenuManager
//Chức năng	: Quản lý menu

class Admin_MenuController extends BaseAdminController {

	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$user->check_login();
		$user->permission();
		$this->count = 0;
	}
	
	public function indexAction(){
		// init
		$tbCurrentPage = new Admin_Model_CurrentPage();
		$tbCategories = new Admin_Model_Categories();
		$tbMenu = new Admin_Model_Menu();
		$tbLanguage = new Language_Model_Language();
		
		//-------------------- get all languages ---------------------//
		$languages = $tbLanguage->getAllLanguages();
		$this->view->languages = $languages;
				
		
		$lang = 0;
		$lang_name = 'Default';
		if(isset($_GET['lang'])&& is_numeric($_GET['lang']))
			$lang = $_GET['lang'];
		if($lang != 0){
			$temp_name = $tbLanguage->getNameLanguage($lang);
			if(count($temp_name) > 0)
				$lang_name = $temp_name['name'];
		}
		//Get all categories
		
		//Get all pages
		$temp = $tbCurrentPage->selectAllPage();
		if(count($temp) > 0)
		{
			foreach ($temp as $r)
			{
				$title_lang=unserialize($r['title']);
				$r['title']=$title_lang[$lang_name];
				$allPages[] = array(
				'id'	=>	$r['page_id'],
				'name'	=> 	$r['title'],
				'link'	=>	$r['link']
				);
			}
			
		}
		else
		{
			$allPages = NULL;
		}
		
		//end get all pages
		// Product
		$temp = $tbCategories->selectCateByType(1);
		if(count($temp) > 0){
			foreach ($temp as $r){
			$t_name = unserialize($r['category_name']);
			$allProduct[] = array(
						'id'	=>	$r['category_id'],
						'name'	=> 	isset($t_name[$lang_name])?$t_name[$lang_name]:$t_name['Default'],
						'link'	=>	'/index/category?category='.$r['category_id']
						);
			}
		}
		else
		{
			$allProduct = NULL;
		}

		// New
		$temp = $tbCategories->selectCateByType(2);
		if(count($temp) > 0){
			foreach ($temp as $r){
			$t_name = unserialize($r['category_name']);
			$allNew[] = array(
						'id'	=>	$r['category_id'],
						'name'	=> 	isset($t_name[$lang_name])?$t_name[$lang_name]:$t_name['Default'],
						'link'	=>	'/news/category?category='.$r['category_id']
					);
			}
		}else{
			$allNew = NULL;
		}

		// QA
		$temp = $tbCategories->selectCateByType(3);
		if(count($temp) > 0){
			foreach ($temp as $r){
			$t_name = unserialize($r['category_name']);
			$allQA[] = array(
						'id'	=>	$r['category_id'],
						'name'	=> 	isset($t_name[$lang_name])?$t_name[$lang_name]:$t_name['Default'],
						'link'	=>	'/qa/category?category='.$r['category_id']
						);
			}
		}else{
			$allQA = NULL;
		}		
		
		//end get all categories
		
		//Get value of menu
		$Menus = $this->findMenuItem($tbMenu, 0,$lang);
		if(count($Menus) > 0 )
		{	
			$list = array();
			$this->tranferArray($Menus,&$list);
			$allMenu = $list;
		}else{
			$allMenu = NULL;
		}
		//return values to view
		$this->view->allPages 	= $allPages;
		$this->view->allProduct = $allProduct;
		$this->view->allNew 	= $allNew;
		$this->view->allQA 		= $allQA;
		$this->view->allMenu	= $allMenu;
		$this->view->lang		= $lang;
	}
	
	// ham de quy tim menu con
	public function findMenuItem($table,$parent_id,$lang,$depth = 0,$cate = NULL){
		if(!$cate) $cate = array();
		$data = $table->selectMenuItemByParentId($parent_id,$lang);
	   if(empty($data))
   		{
    	 return;
   		}
		foreach ($data as $d){
			$cate[] = array(
								'id'	=>	$d['menu_id'],
								'title'	=>	$d['title'],
								'name'	=>	$d['name'],
								'type'	=>	$d['type'],
								'link'	=>	$d['link'],
								'parent_id'	=>	$d['parent_id'],
								'depth'	=>	$depth,
								'child'	=>	$this->findMenuItem($table, $d['menu_id'],$lang,$depth + 1)
							);
		}
		return $cate;
	}
	
	//ham de quy thanh 1 mang chua cate theo thu tu
	public function tranferArray($array,$list){
		foreach ($array as $r){
			if(count($r['child']) > 0 ){
				$list[] = array_slice($r, 0, 7); 
				$this->tranferArray($r['child'],&$list);				
			}else{
				$list[] = array_slice($r, 0, 7);
			}
		}
		
	}
	
	//ham xu ly luu database
	public function successAction(){
		if($this->_request->isPost())
		{
			$tbMenu = new Admin_Model_Menu();
			$check = 1;
			if(isset($_POST['menu-item-db-id']) && count($_POST['menu-item-db-id']) > 0 && isset($_POST['current_language'])){
				$lang = $_POST['current_language'];
				if($tbMenu->deleteNotInMenuItem($_POST['menu-item-db-id'],$lang) != -1 ){
					$count = 1;
					foreach ($_POST['menu-item-db-id'] as $id){

						if($_POST['menu-item-type'][$id] != 'custom')
						{
							$temp = array(
									'title'			=> 	$_POST['menu-item-title'][$id],
									'status'		=>	'1',
									'parent_id'		=>	$_POST['menu-item-parent-id'][$id],
									'language_id'	=>	$lang,
									'sort'			=> $count
								);
						}else
						{
							$temp = array(
									'title'			=> 	$_POST['menu-item-title'][$id],
									'link'			=> 	$_POST['menu-item-url'][$id],
									'status'		=>	'1',
									'parent_id'		=>	$_POST['menu-item-parent-id'][$id],
									'language_id'	=>	$lang,
									'sort'			=> $count
								);						
						}
						if($tbMenu->updateMenuItem($id,$temp) == -1)
						{
							$check = 0;
						}
						$count++;
					}					
				}
				else
				{
					$check = 0;
				}

			}
			else
			{
				if($tbMenu->deleteAllMenuItem($lang) == -1)
				{
					$check = 0;
				}
			}
			$this->view->message = $check;
		}else{
			$this->_redirect("/admin/menu/index");
		}
	}
	
	//ham xu ly ajax
	public function ajaxAction()
	{
		$this->_helper->layout->disableLayout();
		$tbMenu = new Admin_Model_Menu();
		$menu = $_POST['menu-item'];
		$lang = 0;
		if(isset($_GET['lang']))
			$lang = $_GET['lang'];
		if($menu != null){
			foreach ($menu as $data){
				if(count($data) > 0){
					$temp = array(
						'title'			=> 	$data['menu-item-title'],
						'name'			=> 	$data['menu-item-title'],
						'type'			=>	$data['menu-item-type'],
						'link'			=>	$data['menu-item-url'],
						'status'		=>	'0',
						'parent_id'		=>	'0',
						'language_id'	=>	$lang,
						'sort'			=> 0,
						'created_date'	=>	time()
					);
					$menu_item_id = $tbMenu->insertMenuItem($temp);

					if($menu_item_id != -1){						
						$output = $this->make_html($data,$menu_item_id);
						if($output != NULL){
							echo $output;
							die;
						}
						
					}
				}
			}
		}
	}
	
	function make_html($data,$id){
		if($data == null || count($data) < 0){
			return NULL;
		}
		
		$output = "";
		switch ($data['menu-item-type']){
			case 'page' :
				{
					$output = '<li style="position: relative; left: 0px; top: 0pt; margin-top: 0px;" id="menu-item-'.$id.'" class="menu-item menu-item-depth-0 menu-item-page menu-item-edit-inactive pending">
								<dl class="menu-item-bar">
									<dt class="menu-item-handle">
										<span class="item-title">'.$data['menu-item-title'].'</span>
										<span class="item-controls">
											<span class="item-type">Page</span>
											<a class="item-edit" id="edit-'.$id.'" title="Edit Menu Item" href="#menu-item-settings-'.$id.'">Edit Menu Item</a>
										</span>
									</dt>
								</dl>

								<div class="menu-item-settings" id="menu-item-settings-'.$id.'">
									<p class="description description-wide">
										<label for="edit-menu-item-title-'.$id.'">
											Navigation Label<br>
											<input maxlength="45" id="edit-menu-item-title-'.$id.'" class="widefat edit-menu-item-title" name="menu-item-title['.$id.']" value="'.$data['menu-item-title'].'" type="text">
										</label>
									</p>

									<div class="menu-item-actions description-wide submitbox">
										<p class="link-to-original">Original: <a href="'.$data['menu-item-url'].'" target="_blank">'.$data['menu-item-title'].'</a></p>
										<a class="item-delete submitdelete deletion" id="delete-'.$id.'" href="">Delete</a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-'.$id.'" href="">Cancel</a>
									</div>

									<input class="menu-item-data-db-id" name="menu-item-db-id['.$id.']" value="'.$id.'" type="hidden">
									<input class="menu-item-data-parent-id" name="menu-item-parent-id['.$id.']" value="0" type="hidden">
									<input class="menu-item-data-type" name="menu-item-type['.$id.']" value="page" type="hidden">
								</div><!-- .menu-item-settings-->
								
								<ul class="menu-item-transport"></ul>
								
							</li>';
					break;
				}
			case 'cate' :
				{
					$output = '<li style="position: relative; left: 0px; top: 0pt; margin-top: 0px;" id="menu-item-'.$id.'" class="menu-item menu-item-depth-0 menu-item-page menu-item-edit-inactive pending">
								<dl class="menu-item-bar">
									<dt class="menu-item-handle">
										<span class="item-title">'.$data['menu-item-title'].'</span>
										<span class="item-controls">
											<span class="item-type">CATEGORY</span>
											<a class="item-edit" id="edit-'.$id.'" title="Edit Menu Item" href="#menu-item-settings-'.$id.'">Edit Menu Item</a>
										</span>
									</dt>
								</dl>

								<div class="menu-item-settings" id="menu-item-settings-'.$id.'">
									<p class="description description-wide">
										<label for="edit-menu-item-title-'.$id.'">
											Navigation Label<br>
											<input maxlength="45" id="edit-menu-item-title-'.$id.'" class="widefat edit-menu-item-title" name="menu-item-title['.$id.']" value="'.$data['menu-item-title'].'" type="text">
										</label>
									</p>

									<div class="menu-item-actions description-wide submitbox">
										<p class="link-to-original">Original: <a href="'.$data['menu-item-url'].'" target="_blank">'.$data['menu-item-title'].'</a></p>
										<a class="item-delete submitdelete deletion" id="delete-'.$id.'" href="">Delete</a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-'.$id.'" href="">Cancel</a>
									</div>

									<input class="menu-item-data-db-id" name="menu-item-db-id['.$id.']" value="'.$id.'" type="hidden">
									<input class="menu-item-data-parent-id" name="menu-item-parent-id['.$id.']" value="0" type="hidden">
									<input class="menu-item-data-type" name="menu-item-type['.$id.']" value="cate" type="hidden">
								</div><!-- .menu-item-settings-->
								
								<ul class="menu-item-transport"></ul>
								
							</li>';
					break;
				}
			case 'custom' :
				{
					$output = '<li style="position: relative; left: 0px; top: 0pt;" id="menu-item-'.$id.'" class="menu-item menu-item-custom menu-item-edit-inactive pending menu-item-depth-0">
								<dl class="menu-item-bar">
									<dt class="menu-item-handle">
										<span class="item-title">'.$data['menu-item-title'].'</span>
										<span class="item-controls">
											<span class="item-type">Custom</span>
											<a class="item-edit" id="edit-'.$id.'" title="Edit Menu Item" href="#menu-item-settings-'.$id.'">Edit Menu Item</a>
										</span>
									</dt>
								</dl>

								<div class="menu-item-settings" id="menu-item-settings-'.$id.'">
										<p class="field-url description description-wide">
											<label for="edit-menu-item-url-'.$id.'">
												Link<br>
												<input maxlength="200" id="edit-menu-item-url-'.$id.'" class="widefat code edit-menu-item-url" name="menu-item-url['.$id.']" value="'.$data['menu-item-url'].'" type="text">
											</label>
										</p>
										<p class="description description-wide">
										<label for="edit-menu-item-title-'.$id.'">
											Navigation Label<br>
											<input maxlength="45" id="edit-menu-item-title-'.$id.'" class="widefat edit-menu-item-title" name="menu-item-title['.$id.']" value="'.$data['menu-item-title'].'" type="text">
										</label>
										</p>


									<div class="menu-item-actions description-wide submitbox">
										<a class="item-delete submitdelete deletion" id="delete-'.$id.'" href="">Delete</a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-'.$id.'" href="">Cancel</a>
									</div>

									<input class="menu-item-data-db-id" name="menu-item-db-id['.$id.']" value="'.$id.'" type="hidden">
									<input class="menu-item-data-parent-id" name="menu-item-parent-id['.$id.']" value="0" type="hidden">
									<input class="menu-item-data-type" name="menu-item-type['.$id.']" value="custom" type="hidden">
								</div><!-- .menu-item-settings-->
								<ul class="menu-item-transport"></ul>
							</li>';
					break;
				}	
		}
		return $output;
	}
}